﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLULearningNetwork.ActionFilterAttributes;
using DLULearningNetworkModel;
using DLULearningNetwork.ViewModels;
using System.Web.Script.Serialization;
using DLULearningNetwork.Libs;

namespace DLULearningNetwork.Controllers
{
    public class UserPageController : Controller
    {
        //
        // Undone : Xet quyen cua nguoi thuc hien doi voi tuong
        // Ngoai ra con co mot so quyen khac nhu post len tuong, xem thong tin lop hoc, xem chi tiet tuong
        // O day mac dinh chi co ban be moi co quyen duoc xem va thuc hien cac viet do (chua xu ly private, public , friend)

        [RequireUserLogin()]
        public ActionResult Home([Bind(Prefix = "id")] string nickname)
        {
            if (nickname == "{AccountPostAvatar}")
                return null;
            //Kiem tra cac quyen ma current user co the doi voi username(nguoi so huu trang nay).

            //Render cac noi dung phu hop voi ca quyen
            Account accountOwner = Account.Single(nickname);
            Account curUser = RequireUserLogin.LoginUser;
            Profile profileOfAccountOwner = Profile.Single(accountOwner.AccountID_u);
            if (accountOwner == null)
            {
                return View("NoUser");
            }
            ViewBag.AccountOwner = accountOwner;
            ViewBag.CurUser = curUser;
            bool isFriend = Friend.IsFriend(accountOwner.AccountID_u, curUser.AccountID_u);
            ViewBag.ShowMakeFriend = !isFriend;
            if (accountOwner.AccountID_u == curUser.AccountID_u)
            {
                ViewBag.ShowMakeFriend = false;
            }
            PrivacyFlag postWallDetailFlag = PrivacyFlag.SelectByFlagType(profileOfAccountOwner.ProfileID_u, EnumPrivacyFlagType.PostWallDetail);
            bool postWallDetail = true;
            if (postWallDetailFlag.VisibilityLevelReference.Value.Name_s == EnumVisibilityLevels.Private.ToString())
            {
                if (accountOwner.AccountID_u != curUser.AccountID_u)
                {
                    postWallDetail = false;
                }
            }
            else
            {
                if (postWallDetailFlag.VisibilityLevelReference.Value.Name_s == EnumVisibilityLevels.Friends.ToString())
                {
                    if (!isFriend)
                    {
                        postWallDetail = false;
                    }
                }
            }
            ViewBag.PostWallDetail = postWallDetail;
            return View();

        }

        [RequireUserLogin()]
        public ActionResult UserHome([Bind(Prefix = "id")]string nickname)
        {
            //Ai đang đăng nhập
            Account currentUser = RequireUserLogin.LoginUser;
            if (string.IsNullOrEmpty(nickname))
            {
                nickname = currentUser.Nickname_s;
            }

            //Undone : hien tai ai cung co quyen binh luan, va chi duoc phep thi moi co the viet bai len tuong
            //nickname là biệt danh của người sở hữu trang này
            Account accountOwner = Account.Single(nickname);
            ViewBag.AccountOwner = accountOwner;


            //Neu la dang nhap vao chinh trang cua minh thi moi quyen deu co the va khong hien thi nut ket ban
            if (accountOwner.AccountID_u == currentUser.AccountID_u)
            {
                ViewBag.ShowMakeFriend = false;
                ViewBag.ShowTextBoxMainStatus = true;
                ViewBag.AllowComment = true;
                return View();
            }
            else
            {
                //Kiem tra la ban be khong
                bool isFriend = false;
                Friend friendRelation = Friend.Single(accountOwner.AccountID_u, currentUser.AccountID_u);
                if (friendRelation != null)
                {
                    if (friendRelation.Status_i == (int)EnumFriendStatus.Accept)
                    {
                        isFriend = true;
                        ViewBag.AllowComment = true;
                    }
                    else
                    {
                        // Chi duoc accept khi current user la nguoi duoc moi
                        if (friendRelation.FriendAccountID_u == currentUser.AccountID_u)
                        {
                            ViewBag.FriendIDWaiting = friendRelation.FriendID_u;
                        }
                    }
                }
                
                ViewBag.ShowMakeFriend = (ViewBag.FriendIDWaiting == null) && !isFriend;
                if (friendRelation != null && friendRelation.Status_i == (int)EnumFriendStatus.Waitting)
                {
                    ViewBag.ShowMakeFriend = false;
                    ViewBag.IsWaiting = true;
                }
                //Kiem tra co quyen post bai khong
                ViewBag.ShowTextBoxMainStatus = CheckIfPostOnWall(accountOwner.AccountID_u, isFriend);
            }

            return View();
        }

        [RequireUserLogin()]
        public ActionResult GetListFeed(string accountOwnerID, int numberOfSkip)
        {
            //Undone : ai co quyen coi post va comment.
            //tam thoi ai cung co the. nhung chi nguoi duoc phep thi moi co quyen post
            Account currentUser = RequireUserLogin.LoginUser;
            Guid _accountOwnerID = Guid.Parse(accountOwnerID);
            Friend friendRelation = Friend.Single(_accountOwnerID, currentUser.AccountID_u);
            if ((friendRelation != null && friendRelation.Status_i == (int)EnumFriendStatus.Accept) || _accountOwnerID == currentUser.AccountID_u)
            {
                ViewBag.AllowComment = true;
            }
            List<Feed> lstFeed = Feed.SelectFeedForAccountWall(_accountOwnerID, numberOfSkip, Constants.DEFAULT_NUMBER_OF_FEED);
            return PartialView("ListFeed", lstFeed);
        }

        [RequireUserLogin()]
        [HttpPost]
        public ActionResult AddNewPost(string accountOwnerID, string content)
        {
            if (StringHelper.GetLengthOfContent(content) < 1)
            {
                return PartialView("ErrorMessage", "unsuccess/0");
            }
            if (StringHelper.GetLengthOfContent(content) > 5000)
            {
                return PartialView("ErrorMessage", "unsuccess/1");
            }
            //Kiem tra co quyen post tren tuong cua account Owner khong
            Guid _accountOwnerID = Guid.Parse(accountOwnerID);
            Account currentUser = RequireUserLogin.LoginUser;
            Friend friendRelation = Friend.Single(currentUser.AccountID_u, _accountOwnerID);
            bool isFriend;
            if ((isFriend = (friendRelation != null && friendRelation.Status_i == (int)EnumFriendStatus.Accept)) || _accountOwnerID == currentUser.AccountID_u)
            {
                if (!isFriend || CheckIfPostOnWall(_accountOwnerID, isFriend))
                {
                    //Them feed post
                    Feed newFeed = new Feed();
                    newFeed.FeedID_u = Guid.NewGuid();
                    newFeed.ActiveAccountID_u = currentUser.AccountID_u;
                    //Neu dang len tuong minh thi accountOwner = active account
                    newFeed.PassiveAccountID_u = _accountOwnerID;
                    newFeed.Content_s = HttpUtility.HtmlDecode(content);
                    newFeed.CreateDate_dt = DateTime.Now;
                    newFeed.LastUpdate_dt = DateTime.Now;
                    newFeed.Type_s = EnumFeedType.PostOnWall.ToString();
                    if (Feed.Add(newFeed) == 1)
                    {
                        //Phai truy van feed vua moi them boi vi khi render co nhung thong tin ma doi tuong moi tao se khong co.
                        newFeed = Feed.SelectByFeedID(newFeed.FeedID_u);
                        if (newFeed != null)
                        {
                            //Undone : Transaction

                            //Them feed for friend cho accountOwner
                            int i = FeedForFriend.AddFeedForFriend(newFeed.FeedID_u, _accountOwnerID);

                            //Tạo Feed hoạt động gần đây trên tường của người post nếu như post lên tường của người khác
                            if (currentUser.AccountID_u != _accountOwnerID)
                            {
                                Feed newFeedPostFriendWall = new Feed();
                                newFeedPostFriendWall.FeedID_u = Guid.NewGuid();
                                newFeedPostFriendWall.ActiveAccountID_u = currentUser.AccountID_u;
                                newFeedPostFriendWall.PassiveAccountID_u = _accountOwnerID;
                                newFeedPostFriendWall.Content_s = newFeed.FeedID_u.ToString() +"|"+ Libs.StringHelper.ShortcutContent(HttpUtility.HtmlDecode(content), 30); ;
                                newFeedPostFriendWall.CreateDate_dt = DateTime.Now;
                                newFeedPostFriendWall.LastUpdate_dt = DateTime.Now;
                                newFeedPostFriendWall.Type_s = EnumFeedType.ActivityPostOnWallFriend.ToString();
                                //Undone : transaction
                                Feed.Add(newFeedPostFriendWall);
                            }
                            // tra ve html cua feed do
                            ViewBag.AllowComment = true;
                            return PartialView("Feed", newFeed);
                        }
                    }
                }
            }
            return PartialView("ErrorMessage", "unsuccess");
        }

        /// <summary>
        /// Kiem tra quyen post bai cua current user doi voi nguoi so huu tuong
        /// </summary>
        /// <param name="accountOwnerID"></param>
        /// <param name="currentUser"></param>
        /// <returns>true : duoc phep, false khong duoc phep</returns>
        bool CheckIfPostOnWall(Guid accountOwnerID, bool isFriend)
        {
            Profile profileOfOwnerAcccount = Profile.Single(accountOwnerID);
            PrivacyFlag privacyFlag = PrivacyFlag.SelectByFlagType(profileOfOwnerAcccount.ProfileID_u, EnumPrivacyFlagType.PostWallDetail);
            string visibilityLevel = privacyFlag.VisibilityLevelReference.Value.Name_s;
            if (visibilityLevel == EnumVisibilityLevels.Public.ToString())
            {
                return true;
            }
            else
            {
                if (visibilityLevel == EnumVisibilityLevels.Friends.ToString())
                {
                    return isFriend;
                }
                else
                {
                    return false;
                }
            }
        }

        [RequireUserLogin()]
        [HttpPost]
        public ActionResult AddNewComment(string accountOwnerID, string feedID, string content)
        {
            if (StringHelper.GetLengthOfContent(content) < 1)
            {
                return PartialView("ErrorMessage", "unsuccess/0");
            }
            if (StringHelper.GetLengthOfContent(content) > 5000)
            {
                return PartialView("ErrorMessage", "unsuccess/1");
            }
            Feed feed = Feed.SelectByFeedID(Guid.Parse(feedID));
            Account currentUser = RequireUserLogin.LoginUser;
            Friend friendRelation = null;
            if (feed != null)
            {
                if ( feed.Type_s == EnumFeedType.PostOnWall.ToString())
                {
                    if (Guid.Parse(accountOwnerID) != currentUser.AccountID_u)
                    {
                        friendRelation = Friend.Single(currentUser.AccountID_u, Guid.Parse(accountOwnerID));
                        if (friendRelation == null || friendRelation.Status_i != (int)EnumFriendStatus.Accept)
                        {
                            return PartialView("ErrorMessage", "unsuccess");
                        }
                    }

                    FeedComment fcmt = new FeedComment();
                    fcmt.ActiveAccount_u = RequireUserLogin.LoginUser.AccountID_u;
                    fcmt.FeedCommentID_u = Guid.NewGuid();
                    fcmt.Content_s = HttpUtility.HtmlDecode(content);
                    fcmt.FeedID_u = feed.FeedID_u;
                    fcmt.CreateDate_dt = DateTime.Now;
                    if (FeedComment.Add(fcmt) == 1)
                    {
                        fcmt = FeedComment.SelectByFeedCommentID(fcmt.FeedCommentID_u);
                        if (fcmt != null)
                        {
                            //Cap nhat lai lastupdate cua feed
                            Feed.UpdateLastDate(Guid.Parse(feedID));

                            //Them feed nay vao bang FeedForFriend cho Current User,
                            //de nguoi comment tien theo doi khi vao trang home
                            FeedForFriend feedForFriend = new FeedForFriend();
                            feedForFriend.FeedForFriendID_u = Guid.NewGuid();
                            feedForFriend.FeedID_u = feed.FeedID_u;
                            feedForFriend.AccountFriendID_u = fcmt.ActiveAccount_u;
                            int i = FeedForFriend.Add(feedForFriend);

                            // Them feed hoat dong binh luan gan day neu nhu comment tren post thuoc tuong nha nguoi khac
                            if (feed.PassiveAccountID_u != currentUser.AccountID_u)
                            {
                                Feed newFeed = new Feed();
                                newFeed.FeedID_u = Guid.NewGuid();
                                newFeed.ActiveAccountID_u = currentUser.AccountID_u;
                                newFeed.Type_s = EnumFeedType.ActivityComment.ToString();
                                newFeed.CreateDate_dt = DateTime.Now;
                                newFeed.LastUpdate_dt = DateTime.Now;
                                newFeed.CommentID = fcmt.FeedCommentID_u;
                                newFeed.Content_s = StringHelper.ShortcutContent(content,30);
                                //Undone : transaction
                                Feed.Add(newFeed);
                            }
                            return PartialView("FeedComment", fcmt);
                        }
                    }
                }
            }
            return PartialView("ErrorMessage", "unsuccess");
        }

        [RequireUserLogin()]
        public ActionResult GetListFeedComment(string feedID, int numberOfSkip)
        {
            List<FeedComment> lstFeedComment = FeedComment.SelectByFeedID(Guid.Parse(feedID), numberOfSkip, Constants.DEFAULT_NUMBER_OF_FEEDCOMMENT);
            lstFeedComment = lstFeedComment.OrderBy(fc => fc.CreateDate_dt).ToList();
            return PartialView("ListFeedComment", lstFeedComment);
        }

        [RequireUserLogin]
        [HttpPost]
        public string DeleteFeed(string feedID)
        {
            Account currentUser = RequireUserLogin.LoginUser;
            Feed feed = Feed.SelectByFeedID(Guid.Parse(feedID));
            if (feed != null)
            {
                if (feed.ActiveAccountID_u == currentUser.AccountID_u || feed.PassiveAccountID_u == currentUser.AccountID_u)
                {
                    if (Feed.DeleteFeed(Guid.Parse(feedID)) == 1)
                    {
                        return "success";
                    }
                }
            }
            return "unsucess";
        }

        [RequireUserLogin]
        [HttpPost]
        public string DeleteFeedComment(string feedCommentID)
        {
            Account currentUser = RequireUserLogin.LoginUser;
            FeedComment fcmt = FeedComment.SelectByFeedCommentID(Guid.Parse(feedCommentID));
            if (fcmt != null)
            {
                if (fcmt.ActiveAccount_u == currentUser.AccountID_u ||
                    fcmt.FeedReference.Value.ActiveAccountID_u == currentUser.AccountID_u)
                {
                    if (FeedComment.Delete(fcmt.FeedCommentID_u) == 1)
                    {
                        return "success";
                    }
                }
            }
            return "unsuccess";
        }

        [RequireUserLogin()]
        public ActionResult Info([Bind(Prefix = "id")] string nickname)
        {
            Account currentUser = RequireUserLogin.LoginUser;
            if (string.IsNullOrEmpty(nickname))
            {
                nickname = currentUser.Nickname_s;
            }
            Account accountOwner = Account.Single(nickname);
            ViewBag.AccountOwner = accountOwner;
            Profile profileOfAccountOwner = Profile.Single(accountOwner.AccountID_u);

            //Kiem tra la ban be khong
            bool isFriend = false;
            Friend friendRelation = Friend.Single(accountOwner.AccountID_u, currentUser.AccountID_u);
            if (friendRelation != null)
            {
                if (friendRelation.Status_i == (int)EnumFriendStatus.Accept)
                {
                    isFriend = true;
                }
                else
                {
                    // Chi duoc accept khi current user la nguoi duoc moi
                    if (friendRelation.FriendAccountID_u == currentUser.AccountID_u)
                    {
                        ViewBag.FriendIDWaiting = friendRelation.FriendID_u;
                    }
                }
            }
            ViewBag.ShowMakeFriend = (ViewBag.FriendIDWaiting == null) && !isFriend;

            ViewBag.IsFriend = isFriend;
            List<PrivacyFlag> lstPrivacyFlag = PrivacyFlag.SelectByProfileID(profileOfAccountOwner.ProfileID_u);

            //Kiem tra xem thong tin nao duoc phep hien thi doi voi current user
            //Neu accountOwner la curUser thi VIewBag.Share = null, show tat ca
            if (accountOwner.AccountID_u == currentUser.AccountID_u)
            {
                ViewBag.ShareBasicInfo = true;
                ViewBag.ShareContactInfo = true;
                ViewBag.ShareEducationInfo = true;
                ViewBag.ShareOccupationInfo = true;
                ViewBag.ShareAccomodationInfo = true;
                ViewBag.ShareInterestInfo = true;
                ViewBag.ShowMakeFriend = false;
            }
            //Kiem tra co hien thi thong tin hay khong
            else
            {
                ViewBag.ShareBasicInfo = CheckIsDisplay(lstPrivacyFlag, EnumAttributeType.BasicInfo, isFriend);
                ViewBag.ShareContactInfo = CheckIsDisplay(lstPrivacyFlag, EnumAttributeType.ContactInfo, isFriend);
                ViewBag.ShareEducationInfo = CheckIsDisplay(lstPrivacyFlag, EnumAttributeType.EducationInfo, isFriend);
                ViewBag.ShareOccupationInfo = CheckIsDisplay(lstPrivacyFlag, EnumAttributeType.OccupationInfo, isFriend);
                ViewBag.ShareAccomodationInfo = CheckIsDisplay(lstPrivacyFlag, EnumAttributeType.AccomodationInfo, isFriend);
                ViewBag.ShareInterestInfo = CheckIsDisplay(lstPrivacyFlag, EnumAttributeType.InterestInfo, isFriend);
                ViewBag.ShowMakeFriend = !isFriend;
            }
            //Lay thong tin
            ViewBag.AccountOwner = accountOwner;

            if (ViewBag.ShareContactInfo)
            {
                ViewBag.ProfileOfAccountOwner = profileOfAccountOwner;
                if (profileOfAccountOwner.IMYahoo_s == null
                    && profileOfAccountOwner.IMSkype_s == null
                    && profileOfAccountOwner.IMMSN_s == null
                    && profileOfAccountOwner.IMGTalk_s == null
                    && profileOfAccountOwner.IMAOL_s == null)
                {
                    ViewBag.ShareContactInfo = false;
                }
            }
            if (ViewBag.ShareEducationInfo)
            {
                ViewBag.LstEducationInfoVM = GetDataForAttribute(profileOfAccountOwner.ProfileID_u, EnumAttributeType.EducationInfo);

            }
            if (ViewBag.ShareOccupationInfo)
            {
                ViewBag.LstOccupationInfoVM = GetDataForAttribute(profileOfAccountOwner.ProfileID_u, EnumAttributeType.OccupationInfo);
            }
            if (ViewBag.ShareAccomodationInfo)
            {
                ViewBag.LstAccomodationInfoVM = GetDataForAttribute(profileOfAccountOwner.ProfileID_u, EnumAttributeType.AccomodationInfo);
            }
            if (ViewBag.ShareInterestInfo)
            {
                ProfileAttribute interestAttribute = ProfileAttribute.SelectByProfileIdAndAttributeType(
                                                                    profileOfAccountOwner.ProfileID_u,
                                                                    EnumAttributeType.InterestInfo.ToString()).FirstOrDefault();
                if (interestAttribute != null)
                {
                    ViewBag.InterestInfo = interestAttribute.Response_s.Replace("\n", "<br>");
                }
                else
                {
                    ViewBag.InterestInfo = "";
                }
            }

            return View();
        }

        [RequireUserLogin()]
        public ActionResult FriendList([Bind(Prefix = "id")] string nickname)
        {
            Account curUser = RequireUserLogin.LoginUser;
            if (string.IsNullOrEmpty(nickname))
            {
                nickname = curUser.Nickname_s;
            }
            Account accountOwner = Account.Single(nickname);
            if (accountOwner == null)
            {
                ViewBag.ErrorMessage = "Không có người dùng này trong hệ thống";
                return View("Error");
            }
            ViewBag.AccountOwner = accountOwner;
            Profile profileOfAccountOwner = Profile.Single(accountOwner.AccountID_u);

            bool isFriend = Friend.IsFriend(accountOwner.AccountID_u, curUser.AccountID_u);
            bool showMakeFriend = !isFriend;
            bool showFriendList = IsPermit(accountOwner, curUser, isFriend, profileOfAccountOwner.ProfileID_u, EnumPrivacyFlagType.ShowFriendList);

            if (accountOwner.AccountID_u == curUser.AccountID_u)
            {
                showMakeFriend = false;
                showFriendList = true;
            }
            //Kiem tra quyen xem danh sach ban be

            int totalItems = 0;
            if (showFriendList)
            {
                //Lay danh sach ban be
                List<Friend> lstFriend = Friend.SelectFriendOfAnAccount(accountOwner.AccountID_u, 1, Constants.DEFAULT_NUMBER_OF_ITEMS_PERPAGE, ref totalItems);
                ViewBag.ListFriend = lstFriend;
            }
            ViewBag.ShowFriendList = showFriendList;
            ViewBag.ShowMakeFriend = showMakeFriend;
            ViewBag.AccountOwner = accountOwner;
            ViewBag.CurUser = curUser;
            ViewBag.TotalItems = totalItems;
            ViewBag.CurrentPage = 1;
            return View();
        }

        [HttpPost]
        [RequireUserLogin()]
        public ActionResult FriendList(string nickname, int page)
        {
            Account curUser = RequireUserLogin.LoginUser;
            if (string.IsNullOrEmpty(nickname))
            {
                nickname = curUser.Nickname_s;
            }
            Account accountOwner = Account.Single(nickname);
            Profile profileOfAccountOwner = Profile.Single(accountOwner.AccountID_u);

            bool isFriend = Friend.IsFriend(accountOwner.AccountID_u, curUser.AccountID_u);
            bool showMakeFriend = !isFriend;
            bool showFriendList = IsPermit(accountOwner, curUser, isFriend, profileOfAccountOwner.ProfileID_u, EnumPrivacyFlagType.ShowFriendList);
            if (accountOwner.AccountID_u == curUser.AccountID_u)
            {
                showMakeFriend = false;
                showFriendList = true;
            }
            //Kiem tra quyen xem danh sach ban be
            List<Friend> lstFriend = null;
            int totalItems = 0;
            if (showFriendList)
            {
                //Lay danh sach ban be
                lstFriend = Friend.SelectFriendOfAnAccount(accountOwner.AccountID_u, page, Constants.DEFAULT_NUMBER_OF_ITEMS_PERPAGE, ref totalItems);
                ViewBag.ListFriend = lstFriend;
            }
            ViewBag.ShowFriendList = showFriendList;
            ViewBag.ShowMakeFriend = showMakeFriend;
            ViewBag.AccountOwner = accountOwner;
            ViewBag.CurUser = curUser;
            ViewBag.TotalItems = totalItems;
            ViewBag.CurrentPage = page;
            return PartialView("PartialListFriend", lstFriend);
        }

        string DisplayTimeWallDetail(DateTime dateTimeOfWallDetail)
        {
            string strTime = string.Empty;
            TimeSpan timeSpan = DateTime.Now.Subtract(dateTimeOfWallDetail);
            if (timeSpan.TotalDays > 7)
            {
                strTime = string.Format("{0: HH:mm dd/MM/YY}", dateTimeOfWallDetail);
            }
            else
            {
                if (timeSpan.TotalHours > 23)
                {
                    strTime = string.Format("{0: HH:mm ddd}", dateTimeOfWallDetail);
                }
                else
                {
                    if (timeSpan.TotalMinutes > 59)
                    {
                        strTime = string.Format("{0} giờ trước", (int)timeSpan.TotalHours);
                    }
                    else
                    {
                        if (timeSpan.TotalMilliseconds > 59)
                        {
                            strTime = string.Format("{0} phút trước", (int)timeSpan.TotalMinutes);
                        }
                        else
                        {
                            strTime = string.Format("{0} giây trước", (int)timeSpan.TotalSeconds);
                        }
                    }
                }
            }
            return strTime;
        }

        //ChildCommentViewModel CreateNewChildCommentViewModel(ChildComment childComment)
        //{
        //    ChildCommentViewModel ccVM = new ChildCommentViewModel();
        //    ccVM.ChildCommentID = childComment.ID_u.ToString();
        //    //ccVM.AccountPostAvatar = childComment.AccountReference.Value.Avatar_s;
        //    if (ccVM.AccountPostAvatar == null || ccVM.AccountPostAvatar == "")
        //    {
        //        ccVM.AccountPostAvatar = @"/Content/Images/avatar.gif";
        //    }
        //    //ccVM.AccountPostName = childComment.AccountReference.Value.FirstName_s;
        //    //ccVM.AccountPostNickName = childComment.AccountReference.Value.Nickname_s;
        //    ccVM.Content = childComment.Content_s;
        //    ccVM.Time = DisplayTimeWallDetail(childComment.Date_dt);
        //    return ccVM;
        //}

        Object GetDataForAttribute(Guid ProfileID, EnumAttributeType attributeType)
        {
            List<ProfileAttribute> lstEducationAttribute = ProfileAttribute.SelectByProfileIdAndAttributeType(
                                                                        ProfileID,
                                                                        attributeType.ToString());
            JavaScriptSerializer sr = new JavaScriptSerializer();
            switch (attributeType)
            {
                case EnumAttributeType.EducationInfo:
                    List<EducationInfoViewModel> lstEductaionInfoVM = new List<EducationInfoViewModel>();
                    EducationInfoViewModel educationInfoVM;
                    foreach (ProfileAttribute pa in lstEducationAttribute)
                    {
                        educationInfoVM = sr.Deserialize(pa.Response_s, typeof(EducationInfoViewModel)) as EducationInfoViewModel;
                        lstEductaionInfoVM.Add(educationInfoVM);
                    }
                    return lstEductaionInfoVM;
                case EnumAttributeType.AccomodationInfo:
                    List<AccomodationInfoViewModel> lstAccomodationInfoVM = new List<AccomodationInfoViewModel>();
                    AccomodationInfoViewModel accomodationInfoVM;
                    foreach (ProfileAttribute pa in lstEducationAttribute)
                    {
                        accomodationInfoVM = sr.Deserialize(pa.Response_s, typeof(AccomodationInfoViewModel)) as AccomodationInfoViewModel;
                        lstAccomodationInfoVM.Add(accomodationInfoVM);
                    }
                    return lstAccomodationInfoVM;
                case EnumAttributeType.OccupationInfo:
                    List<OccupationInfoViewModel> lstOccupationInfoVM = new List<OccupationInfoViewModel>();
                    OccupationInfoViewModel occupationInfoVM;
                    foreach (ProfileAttribute pa in lstEducationAttribute)
                    {
                        occupationInfoVM = sr.Deserialize(pa.Response_s, typeof(OccupationInfoViewModel)) as OccupationInfoViewModel;
                        lstOccupationInfoVM.Add(occupationInfoVM);
                    }
                    return lstOccupationInfoVM;
            }
            return null;
        }

        bool CheckIsDisplay(List<PrivacyFlag> lstPrivacyFlag, EnumAttributeType attributeType, bool? isFriend)
        {
            PrivacyFlag flag = lstPrivacyFlag.SingleOrDefault(f => f.PrivacyFlagTypeReference.Value.FieldName_s == attributeType.ToString());
            if (flag.VisibilityLevelReference.Value.Name_s == EnumVisibilityLevels.Public.ToString())
            {
                return true;
            }
            else
            {
                if (flag.VisibilityLevelReference.Value.Name_s == EnumVisibilityLevels.Public.ToString() && isFriend.Value)
                {
                    return true;
                }
            }
            return false;
        }

        bool IsPermit(Account accountOwner, Account curUser, bool isFriend, Guid profileIdOfCurUser, EnumPrivacyFlagType enumPrivacyFlagType)
        {
            bool isPermit = true;
            PrivacyFlag flag = PrivacyFlag.SelectByFlagType(profileIdOfCurUser, enumPrivacyFlagType);
            if (flag.VisibilityLevelReference.Value.Name_s == EnumVisibilityLevels.Private.ToString())
            {
                if (accountOwner.AccountID_u != curUser.AccountID_u)
                {
                    isPermit = false;
                }
            }
            else
            {
                if (flag.VisibilityLevelReference.Value.Name_s == EnumVisibilityLevels.Friends.ToString())
                {
                    if (!isFriend)
                    {
                        isPermit = false;
                    }
                }
            }
            return isPermit;
        }

        //[RequireUserLogin()]
        //public ActionResult FeedDetails(string feedID)
        //{
        //    Feed feed = Feed.SelectByFeedID(Guid.Parse(feedID));
        //    if (feedID == null)
        //    {
        //        return View("ErrorMessage", "Không còn hoạt động này nữa");
        //    }
        //    if(feed.Type_s == EnumFeedType.)
        //}
    }
}
